package com.dqhl.community.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.dqhl.community.R;
import com.dqhl.community.adapter.CouponsPagerAdapter;
import com.dqhl.community.base.BaseActivity;
import com.dqhl.community.fragment.CouponsFragment;
import com.dqhl.community.util.Constant;
import com.dqhl.community.view.IndexViewPager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cheng on 2016/8/30.
 * 钱包 activity
 */
public class WalletActivity extends BaseActivity implements View.OnClickListener {

    private Context context;
    private ImageView ivTopBack;
    private TextView tvTopTitle;

    private TextView tvMemberPointsRules; //积分规则
    private TextView tvMemberPointsDetails; //您的积分明细

    private TextView tvUnused; // 未使用
    private TextView tvUsed; // 已使用
    private TextView tvOverdue; // 已过期
//    private View lineUnused; //未使用下面的横线
//    private View lineUsed; //已使用下面的横线
//    private View lineOverdue; //已过期下面的横线
    private IndexViewPager couponsViewPager;
    private List<Fragment> fragmentList = new ArrayList<>();
    private CouponsPagerAdapter couponsPagerAdapter;
    private static final int TAG_UNUSED = 0;
    private static final int TAG_USED = 1;
    private static final int TAG_OVERDUE = 2;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wallet);
        context = this;

        initView();
    }

    private void initView() {
        ivTopBack = (ImageView) findViewById(R.id.iv_top_back);
        ivTopBack.setVisibility(View.VISIBLE);
        tvTopTitle = (TextView) findViewById(R.id.tv_top_title);
        tvTopTitle.setText(R.string.txt_wallet);

        tvMemberPointsRules = (TextView) findViewById(R.id.tv_member_points_rules);
        tvMemberPointsDetails = (TextView) findViewById(R.id.tv_member_points_details);

        tvUnused = (TextView) findViewById(R.id.tv_unused);
        tvUsed = (TextView) findViewById(R.id.tv_used);
        tvOverdue = (TextView) findViewById(R.id.tv_overdue);
//        lineUnused = findViewById(R.id.line_unused);
//        lineUsed = findViewById(R.id.line_used);
//        lineOverdue = findViewById(R.id.line_overdue);
        couponsViewPager = (IndexViewPager) findViewById(R.id.vp_coupons);

        ivTopBack.setOnClickListener(this);
        tvMemberPointsRules.setOnClickListener(this);
        tvMemberPointsDetails.setOnClickListener(this);
        tvUnused.setOnClickListener(this);
        tvUsed.setOnClickListener(this);
        tvOverdue.setOnClickListener(this);

        CouponsFragment couponsUnusedFragment = (CouponsFragment) CouponsFragment.newInstance(Constant.COUPONS_UNUSED);
        CouponsFragment couponsUsedFragment = (CouponsFragment) CouponsFragment.newInstance(Constant.COUPONS_USED);
        CouponsFragment couponsOverdueFragment = (CouponsFragment) CouponsFragment.newInstance(Constant.COUPONS_OVERDUE);
        fragmentList.add(couponsUnusedFragment);
        fragmentList.add(couponsUsedFragment);
        fragmentList.add(couponsOverdueFragment);
        couponsPagerAdapter = new CouponsPagerAdapter(getSupportFragmentManager(), fragmentList);
        couponsViewPager.setAdapter(couponsPagerAdapter);
        couponsViewPager.setScanScroll(true); //viewpager设置为可以滑动
        couponsViewPager.addOnPageChangeListener(new MyOnPageChangeListener());
    }

    class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            switch (position) {
                case TAG_UNUSED: //未使用
                    unusedSelected();
                    break;
                case TAG_USED: //已使用
                    usedSelected();
                    break;
                case TAG_OVERDUE: //已过期
                    overdueSelected();
                    break;
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_top_back:
                finish();
                break;
            case R.id.tv_member_points_rules: //跳转到积分规则
                overlay(MemberPointsRulesActivity.class);
                break;
            case R.id.tv_member_points_details: //跳转到您的积分明细
                overlay(MemberPointsDetailsActivity.class);
                break;
            case R.id.tv_unused: //未使用
                couponsViewPager.setCurrentItem(TAG_UNUSED);
                unusedSelected();
                break;
            case R.id.tv_used: //已使用
                couponsViewPager.setCurrentItem(TAG_USED);
                usedSelected();
                break;
            case R.id.tv_overdue: //已过期
                couponsViewPager.setCurrentItem(TAG_OVERDUE);
                overdueSelected();
                break;
            default:
                break;
        }
    }

    /**
     * 未使用为选中状态
     */
    private void unusedSelected(){
        tvUnused.setTextColor(ContextCompat.getColor(context, R.color.colorPrimary));
//        lineUnused.setBackgroundColor(ContextCompat.getColor(context, R.color.colorPrimary));
        tvUsed.setTextColor(ContextCompat.getColor(context, R.color.gray6));
//        lineUsed.setBackgroundColor(ContextCompat.getColor(context, R.color.white));
        tvOverdue.setTextColor(ContextCompat.getColor(context, R.color.gray6));
//        lineOverdue.setBackgroundColor(ContextCompat.getColor(context, R.color.white));
    }

    /**
     * 已使用为选中状态
     */
    private void usedSelected(){
        tvUnused.setTextColor(ContextCompat.getColor(context, R.color.gray6));
//        lineUnused.setBackgroundColor(ContextCompat.getColor(context, R.color.white));
        tvUsed.setTextColor(ContextCompat.getColor(context, R.color.colorPrimary));
//        lineUsed.setBackgroundColor(ContextCompat.getColor(context, R.color.colorPrimary));
        tvOverdue.setTextColor(ContextCompat.getColor(context, R.color.gray6));
//        lineOverdue.setBackgroundColor(ContextCompat.getColor(context, R.color.white));
    }

    /**
     * 已过期为选中状态
     */
    private void overdueSelected(){
        tvUnused.setTextColor(ContextCompat.getColor(context, R.color.gray6));
//        lineUnused.setBackgroundColor(ContextCompat.getColor(context, R.color.white));
        tvUsed.setTextColor(ContextCompat.getColor(context, R.color.gray6));
//        lineUsed.setBackgroundColor(ContextCompat.getColor(context, R.color.white));
        tvOverdue.setTextColor(ContextCompat.getColor(context, R.color.colorPrimary));
//        lineOverdue.setBackgroundColor(ContextCompat.getColor(context, R.color.colorPrimary));
    }

}
